<?php

	// Time zone setting
	date_default_timezone_set( 'Asia/Kolkata' );

	defined( 'APP_PATH' )
		|| define( 'APP_PATH', dirname( __FILE__ ) . '/../' );
	defined( 'INCLUDE_PATH' )
		|| define( 'INCLUDE_PATH', APP_PATH . 'components/' );
	defined( 'PHP_PATH' )
		|| define( 'PHP_PATH', APP_PATH . 'php/' );
	defined( 'TPL_PATH' )
		|| define( 'TPL_PATH', APP_PATH . 'tpl/' );

	require_once( APP_PATH . 'config/settings.php' );
	require_once( INCLUDE_PATH . 'functions.php' );
	require_once( INCLUDE_PATH . 'Loader.php' );

	spl_autoload_register( null, false );
	spl_autoload_extensions( '.php' );
	spl_autoload_register( Array( 'Loader', 'includeClass' ) );


	try {

		// login process
		$userId = 1;
		Url::processUrl();
		$requireFile = Url::getUrlFileUri();
		$arrDomainUri = Url::getSplittedDomainURI();
		$hostName = Url::getHostName();

		defined( 'HOME_PATH' )
			|| define( 'HOME_PATH', "http://www.$hostName/" );
		defined( 'IMAGE_PUBLIC_LOCATION' )
			|| define( 'IMAGE_PUBLIC_LOCATION', HOME_PATH . "media/images/" );

		session_start();

		$fileName = explode( '/', $requireFile );
		$tplFile = str_replace( '/php/', '/tpl/', str_replace( '.php', '.tpl.php', $requireFile ) );
		if( file_exists( $tplFile ) ) {
			$template = new PHPTAL( $tplFile );
			$cssFile = '/css/' . str_replace( '.php', '.css', $fileName[ count( $fileName ) - 1 ] );
			$jsFile = '/js/' . str_replace( '.php', '.js', $fileName[ count( $fileName ) - 1 ] );
			$template->pageCssFile = file_exists( $cssFile ) ? $cssFile : false;
			$template->pageJsFile = file_exists( $jsFile ) ? $jsFile : false;
			$template->pageTitle = ucwords( str_replace( '_', ' ', str_replace( '.php', '', $fileName[ count( $fileName ) - 1 ] ) ) );
			require_once( $requireFile );
			echo $template->execute();
		} else {
			require_once( $requireFile );
		}
	} catch( Exception $ex ) {
		describe( $ex->getMessage() . "\n" . $ex->getTraceAsString() );
	}